﻿@using CodeCamp.Models.Schedule
@using CodeCamp.Models.Shared
@using CodeCamp.Infrastructure.Extensions
@model ScheduleGridEditModel

@using (Html.BeginForm("EditSchedule", "Schedule"))
{
    @Html.HiddenFor(model => model.GridEditAction)
<table class="sessionGrid">
    <tr>
        <td>Timeslot</td>
    @foreach (var roomKV in Model.Rooms.OrderBy(kv => kv.Value.Building).OrderBy(kv => kv.Value.RoomName))
    {
        if (Model.BuildingCount > 1)
        {
        <td>@roomKV.Value.Building<br />@roomKV.Value.RoomName (@roomKV.Value.Capacity)</td>
        }
        else
        {
        <td>@roomKV.Value.RoomName (@roomKV.Value.Capacity)</td>
        }
    }
    </tr>
    @foreach (var timeslotKV in Model.Timeslots.OrderBy(kv => kv.Value.StartTime))
    {
        <tr>
            <td>
            @timeslotKV.Value.StartTime.ToShortTimeString()
            @if (timeslotKV.Value.SessionCount > 0)
            {
                <text> &ndash; @timeslotKV.Value.EndTime.ToShortTimeString()</text>
            }

            </td>
            @if (timeslotKV.Value.SessionCount == 0)
            {
                <td colspan="@Model.Rooms.Count.ToString()">@timeslotKV.Value.Name</td>
            }
            else
            {
                foreach (var cell in timeslotKV.Value.GridCells
                    .OrderBy(c => c.Value.CellRoom.Building)
                    .OrderBy(c => c.Value.CellRoom.RoomName))
                {
                    <td>
                    @{
                    var firstCell = true;
                    foreach (var session in cell.Value.Sessions)
                    {
                        if (firstCell) { firstCell = false; }
                        else
                        { <br /> }
                            <input type="checkbox" value="@session.SessionId.ToString()" name="sessionsToRemove" />
                            @Html.ActionLink(session.Title.TruncateWithElipses(60), "Details", "Sessions", new { eventName = Model.EventShortName, id = session.SessionId }, null)
                    }
                    }
                    </td>
                }
            }
        </tr>
    }
    <tr>
        @{ var colCount = @Model.Rooms.Count + 1; }
        <td colspan="@colCount.ToString()"><input id="RemoveSelected" type="submit" name="submit" value="Remove Selected Session(s)" /></td>
    </tr>
</table>    
}